<?php

namespace app\mec\controller;

use app\mec\model\Node;
use think\Controller;
use think\Db;

class Base extends Controller
{
    public $allow;
    
    public function _initialize()
    {
        if(!session('mid')||!session('mname')){
            $this->redirect(url('mec/login/index'));
        }
        $status = Db::name('mechanism_member')->where('id',session('mid'))->value('mechanism_member_status');
        if($status == 2){
            $this->error('抱歉，该账号被禁用，强制下线','mec/login/loginout');
        }
        
        $this->allow = array(
            'mechanismuser/mechanismuseradd',//添加机构管理员
            'mechanismuser/mechanismuseredit',//编辑机构管理员
            'mechanismuser/index',//编辑机构管理员
            'mechanism/index',//机构列表
            'notice/add',//机构列表
        );
    
        $controller = strtolower(request()->controller());
        $action     = strtolower(request()->action());
        $url = strtolower($controller.'/'.$action);
        foreach ($this->allow as $k => $v) {
            if (strpos($url, $v) !== false) {
               $this->mechanism_id = session('mmid') . $this->getIds($this->getMechanismInfo(),session('mmid'));
            }
        }
        $mechanism_name = Db::table('up_mechanism')->where('mechanism_id', session('mmid'))->value('mechanism_name');
        //传递数据，用于展示机构管理员头像、机构名称等
        
        $this->assign([
            'mname' => session('mname'),
            'portrait' => session('mimage'),
            'mechanism_name' => $mechanism_name,
        ]);
    }

    /**
     * place 三级联动
     * @return \think\response\Json
     */
    public function place()
    {
        $area = new \app\common\place\Area;
        $data = $area->area();
        return json($data);
    }

    /**
     * 极光推送
     * @param $type 1:推送个人  2:推送全体
     * @param $alias 别名 user_id OR token
     * @param $message 推送消息内容
     * @param $extras 扩展字段接受数组
     * @return array
     */
    public function Jpush($type,$alias,$message,$extras, $msg, $code)
    {
//        $alias = (string)$alias;
        import('jpush.autoload', EXTEND_PATH);
        //初始化JPushClient
        $client = new \JPush\Client(config('jpush')['appKey'],config('jpush')['masterSecret']);
        //生成推送Payload构建器
        $push = $client->push();
        //推送平台 'all'  OR  ['ios','android']  OR  'ios','android'
        $push->setPlatform('all');
        //1:推送个人  2:推送全体
        if($type==1){
            $push->addAlias($alias);//按别名推送
        }else{
            $push->addAllAudience();//广播消息推送
        }
        //IOS推送
        $push->iosNotification($message, [
                'alert'=>$message,
                'badge' => '+1',
                'extras' => $extras,
                'sound'=>'default'
            ]
        );
        //Android推送
        $push->androidNotification($message, [
                'alert'=>$message,
                'extras' => $extras
            ]
        );
        if($msg!=''){
            $push->message($msg, [
//            'title' => 'Hello',
                'content_type' => 'text',
                'extras' => [
                    'key' => $code
                ]
            ]);
        }
        return $push->send();
    }
    
    /**
     * 获取所有机构名称&ID&等级&上级机构ID
     * @return false|\PDOStatement|string|\think\Collection
     */
    public function getMechanismInfo(){
        $list = Db::table('up_mechanism')
            ->field('mechanism_id, mechanism_name, mechanism_type, mechanism_pid_id')
            ->where('mechanism_status', 2)
            ->where('mechanism_id', 'neq', 1)
            ->select();
        return $list;
    }
    
    /**
     * 获取本机构的所有下级机构ID
     * @param $arr
     * @param $fid
     */
    function getIds($arr, $fid){
    static $ids = '';
    foreach($arr as $va) {
        if($va['mechanism_pid_id'] == $fid){
            $ids .= ',' . $va['mechanism_id'];
            $this->getIds($arr, $va['mechanism_id']);
//            $ids[] = $va['mechanism_id'];
        }
    }
    return $ids;
    }
}